package com.wdz.config;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class MetaHandler extends MetaObjectHandler {
    private static final Logger logger = LoggerFactory.getLogger(MetaHandler.class);

    /**
     * 新增数据执行
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        System.out.println("自动填充创建时间和修改时间");
        Date date=new Date();
        Format format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("createDate", format.format(date), metaObject);
        this.setFieldValByName("updateDate", format.format(date), metaObject);
        this.setFieldValByName("headphoto","https://dss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2306696130,3636777462&fm=26&gp=0.jpg",metaObject);
    }

    /**
     * 更新数据执行
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        System.out.println("自动填充时间");
        Date date=new Date();
        Format format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.setFieldValByName("updateDate", format.format(date), metaObject);
    }
}
